package com.octopus.sample.bing;

import cn.hutool.core.date.DatePattern;
import com.octopus.core.Octopus;
import com.octopus.core.Request;
import com.octopus.core.WebSite;
import com.octopus.core.downloader.DownloadConfig;
import com.octopus.core.processor.MediaFileDownloadProcessor;
import com.octopus.core.processor.extractor.annotation.Extractor;
import com.octopus.core.processor.extractor.annotation.Link;
import com.octopus.core.processor.extractor.selector.Css;
import com.octopus.core.processor.extractor.selector.Formatter;
import com.octopus.core.processor.extractor.selector.Selector;
import com.octopus.core.processor.extractor.selector.Xpath;
import com.octopus.core.processor.extractor.type.DateType;
import com.octopus.core.processor.matcher.Matchers;
import com.octopus.sample.Constants;
import java.util.Date;
import java.util.List;
import lombok.Data;

/**
 * 下载必应壁纸 https://bing.ioliu.cn/
 *
 * @author shoulai.yang@gmail.com
 * @date 2021/1/15
 */
@Data
@Extractor({
  @Link(
      selector = @Selector(type = Selector.Type.Xpath, value = "//a[text()='下一页']/@href"),
      repeatable = false),
  @Link(
      selector = @Selector(type = Selector.Type.Xpath, value = "//a[@class='ctrl download']/@href"),
      repeatable = false,
      priority = 2)
})
public class BingWallpaper {

  @Css(value = ".container > .item", self = true)
  private List<Wallpaper> wallpapers;

  public static void main(String[] args) {
    Octopus.builder()
        .setThreads(2)
        .addSite(
            WebSite.of("bing.ioliu.cn")
                .setRateLimiter(1, 5)
                .setDownloadConfig(new DownloadConfig()))
        .addProcessor(
            Matchers.HTML,
            BingWallpaper.class,
            r -> {
              System.out.println("获取" + r.getWallpapers().size() + "张壁纸");
            })
        .addProcessor(new MediaFileDownloadProcessor(Constants.DOWNLOAD_DIR + "/wallpapers/bing"))
        .addSeeds(Request.get("https://bing.ioliu.cn/?p=1"))
        .build()
        .start();
  }

  @Extractor
  @Data
  public static class Wallpaper {
    @Css("h3")
    private String title;

    @Css(".calendar em")
    @DateType(pattern = DatePattern.NORM_DATE_PATTERN)
    private Date uploadDate;

    @Css(value = ".mark", attr = "href", formatter = @Formatter(regex = "[\\w\\W]*", format = "https://bing.ioliu.cn%s"))
    private String key;

    @Css(value = "img", attr = "src")
    private String thumbnailSrc;

    @Xpath("//a[@class='ctrl download'][1]/@href")
    private String src;

    @Xpath(
        value = "//a[@class='ctrl download'][1]/em/text()",
        formatter = @Formatter(regex = "(\\d+)x.*", groups = 1))
    private Integer width;

    @Xpath(
        value = "//a[@class='ctrl download'][1]/em/text()",
        formatter = @Formatter(regex = ".*x(\\d+)", groups = 1))
    private Integer height;
  }
}
